package com.github.natholdallas.database

import com.github.natholdallas.common.Role
import jakarta.transaction.Transactional
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query

@Transactional
interface Users : JpaRepository<User, Long> {

    @Query("from User o where o.username = :username and o.password = :password")
    fun findLogin(username: String, password: String): User

    @Query("from User o where o.role = :role")
    fun all(role: Role): List<User>

    @Query("from User o where o.role = :role")
    fun all(role: Role, pageable: Pageable): Page<User>

    @Query("from User o where o.role = 0 or o.role = 1")
    fun allByStaffOrAdmin(pageable: Pageable): Page<User>

    @Query("from User o where o.name LIKE :value or o.tag LIKE :value")
    fun allByTagOrName(value: String): List<User>

    @Query("from User o where o.tag LIKE :value")
    fun allByTag(value: String): List<User>

}


@Transactional
interface Comments : JpaRepository<CommentTable, Long> {

    @Query("from CommentTable o where o.copilot.id = :copilot")
    fun allByCopilot(copilot: Long): List<CommentTable>

}


@Transactional
interface Stars : JpaRepository<StarTable, Long> {

    @Query("from StarTable o where o.user.id = :user")
    fun allByUser(user: Long, pageable: Pageable): Page<StarTable>

    @Query("select count(o) > 0 from StarTable o where o.user.id = :user")
    fun byUser(user: Long): Boolean 

}


@Transactional
interface Topups : JpaRepository<TopupTable, Long> {

    @Query("from TopupTable o where o.user.id = :user")
    fun allByUser(user: Long, pageable: Pageable): Page<TopupTable>

}


@Transactional
interface Orders : JpaRepository<OrderTable, Long> {

    @Query("from OrderTable o where o.copilot.id = :copilot")
    fun allByCopilot(copilot: Long, pageable: Pageable): Page<OrderTable>

    @Query("from OrderTable o where o.user.id = :user")
    fun allByUser(user: Long, pageable: Pageable): Page<OrderTable>

}


interface Logs : JpaRepository<LogTable, Long>
